package it.giuseppetrisciuoglio.lyrics.parser;

import java.io.UnsupportedEncodingException;

import it.giuseppetrisciuoglio.lyrics.exception.LyricsNotFoundException;

import org.htmlparser.NodeFilter;
import org.htmlparser.filters.AndFilter;
import org.htmlparser.filters.HasAttributeFilter;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.util.ParserException;

public class LyricsTimeTestoParser extends AbstractTestoParser {

	//http://www.lyricstime.com/vasco-rossi-senza-parole-lyrics.html
	private static String SITE = "http://www.lyricstime.com/";
	private static String BLANK_CHAR = "-";
	
	@Override
	public String getTesto(String httpResponse) throws ParserException, LyricsNotFoundException {
		NodeFilter filter =  new AndFilter(
		        new TagNameFilter("div"),
		        new HasAttributeFilter("id", "songlyrics"));
		return super.getTestoParse(httpResponse, filter);
	}

	@Override
	public String getURL(String artista, String titolo) throws IllegalArgumentException, UnsupportedEncodingException {
		super.validArtistaAndTitolo(artista, titolo);
		return SITE.concat(cleanArtista(artista.trim().replaceAll(" ", BLANK_CHAR).toLowerCase())).concat(BLANK_CHAR)
				.concat(cleanTitolo(titolo.trim().toLowerCase().replaceAll(" ", BLANK_CHAR))).concat(BLANK_CHAR).concat("lyrics.html");
	}
	
	@Override
	protected String cleanTitolo(String titolo) {
		titolo = super.cleanTitolo(titolo);
		return titolo.replace('\'', '-')
				.replace('.', ' ')
				.replace('(', ' ')
				.replace(')', ' ')
				.replaceAll(" ", "");
	}
	
	@Override
	protected String cleanArtista(String artista) {
		artista = super.cleanArtista(artista);
		return artista.replace('\'', '-')
				.replace('.', ' ')
				.replace('(', ' ')
				.replace(')', ' ')
				.replaceAll(" ", "");
	}
	
}
